import { _decorator, Component, Node, Canvas, Layers, UITransform, instantiate, view, screen, ResolutionPolicy, js } from 'cc';
import { EUILayer } from './EUILayer';
import { PrefabCfg } from '../../../PrefabCfg';
import { G_VIEW_SIZE } from '../../../GCtr';
const { ccclass, property } = _decorator;


class MyLayer {
    public readonly node : Node;

    constructor(public readonly layer: EUILayer, public readonly canvas: Canvas, name: string) {
        const node = this.node = new Node(name);
        node.layer = Layers.Enum.UI_2D;
        node.addComponent(UITransform);
        canvas.node.addChild(node);
    }
}

@ccclass('UIManager')
export class UIManager {
    constructor() {
    }
    
    private _canvas: Canvas = null;
    private _layers: MyLayer[] = [];
    
    public init(canvas: Canvas) {
        this._canvas = canvas;
        this.adapterScreen();
        Object.keys(EUILayer)
            .filter(key => !isNaN(Number(key)))
            .forEach(key => {
                console.log("EUILayer", key, EUILayer[key]);
                const myLayer = new MyLayer(EUILayer[key], canvas, key);
                this._layers.push(myLayer);
        });
        
    }

    adapterScreen() {
        let designSize = view.getDesignResolutionSize();
        let screenWindowSize = screen.windowSize;
        let screenW = screenWindowSize.width;
        let screenH = screenWindowSize.height;

        console.log("designSize", designSize);
        console.log("screenW", screenW);
        console.log("screenH", screenH);

        const isWidthLarger = (screenW / screenH) > (designSize.width / designSize.height);

        let resolutionPolicy = view.getResolutionPolicy();
        let targetPolicy = isWidthLarger ? ResolutionPolicy.FIXED_HEIGHT : ResolutionPolicy.FIXED_WIDTH;
        if (targetPolicy !== resolutionPolicy.getContentStrategy().strategy) {
            // view.setResolutionPolicy(policy);
            view.setDesignResolutionSize(designSize.width, designSize.height, targetPolicy);
            view.emit("canvas-resize");
        }

        
        if (isWidthLarger) {
            G_VIEW_SIZE.width = Math.ceil(designSize.height * screenW / screenH);
            G_VIEW_SIZE.height = designSize.height;
        } else {
            G_VIEW_SIZE.width = designSize.width;
            G_VIEW_SIZE.height = Math.ceil(designSize.width * screenH / screenW);
        }

        console.log(`屏幕${isWidthLarger ? "更宽, 高度适配" : "更高, 宽度适配"} 设计分辨率比例下的屏幕尺寸: ${G_VIEW_SIZE.width}x${G_VIEW_SIZE.height}`);
        return isWidthLarger
    }

    open(uiClass: any) {
        const {bundleName, path} = this.getUIClassInfo(uiClass);

        gCtr.res.loadPrefab(bundleName, path, prefab => {
            let match3Node = instantiate(prefab);
            this._layers[EUILayer.UI].node.addChild(match3Node);
            match3Node.getComponent(UITransform).setContentSize(G_VIEW_SIZE.clone());
        });
    }
}


const g_uiCfg = new Map<any, IBundleUrl>();

export function registerUI(cfg: {[uiClassName: string]: IBundleUrl}) {
    for (let uiClassName in cfg) {
        g_uiCfg.set(uiClassName, cfg[uiClassName]);
    }
}

export function getUI(uiClass: any): IBundleUrl {
    if (g_uiCfg.has(uiClass)) {
        return g_uiCfg.get(uiClass);
    }

    let uiClassName = js.getClassName(uiClass);
    let bUrl = PrefabCfg[uiClassName];
    if (!bUrl) {
        debugger;
        console.error(`getUI error: ${uiClassName}`);
        return null;
    }

    g_uiCfg.set(uiClass, bUrl);
    return bUrl;
}

